﻿using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;

namespace Rafy.Book.ViewModels
{
    public class PracticePageViewModel : BindableBase
    {
        private readonly Random _random = new();

        // 全部练习项（可从后端或本地获取）
        private readonly List<PracticeItem> _allItems = new()
        {
            new PracticeItem { Pinyin = "zuò tán huì", Word = "座谈会" },
            new PracticeItem { Pinyin = "zhōng guó rén", Word = "中国人" },
            new PracticeItem { Pinyin = "kě jiàn xìng", Word = "可见性" },
            new PracticeItem { Pinyin = "chuán chéng", Word = "传承" },
            new PracticeItem { Pinyin = "fā zhǎn", Word = "发展" },
            // 添加更多练习数据
        };
        // 当前题目
        private PracticeItem _currentQuestion;
        public PracticeItem CurrentQuestion
        {
            get => _currentQuestion;
            set => SetProperty(ref _currentQuestion, value);
        }
        // 当前选项
        public ObservableCollection<PracticeOption> Options { get; } = new();
        // 题干只显示拼音或只显示汉字
        private bool _isPinyinPractice;
        public bool IsPinyinPractice
        {
            get => _isPinyinPractice;
            set
            {
                if (SetProperty(ref _isPinyinPractice, value))
                    LoadNextQuestion();
            }
        }

        private bool _isWordPractice;
        public bool IsWordPractice
        {
            get => _isWordPractice;
            set
            {
                if (SetProperty(ref _isWordPractice, value))
                    LoadNextQuestion();
            }
        }

        public ICommand OptionSelectedCommand { get; }
        public PracticePageViewModel()
        {
            OptionSelectedCommand = new DelegateCommand<PracticeOption>(OnOptionSelected);
            IsPinyinPractice = true; // 默认显示拼音题
            LoadNextQuestion();
        }
        private void LoadNextQuestion()
        {
            // 随机选择一个题目
            CurrentQuestion = _allItems[_random.Next(_allItems.Count)];

            // 生成选项，包含1个正确答案和3个干扰项
            var correct = new PracticeOption
            {
                Text = IsPinyinPractice ? CurrentQuestion.Word : CurrentQuestion.Pinyin,
                IsCorrect = true
            };

            var distractors = _allItems
                .Where(i => i != CurrentQuestion)
                .OrderBy(_ => _random.Next())
                .Take(3)
                .Select(i => new PracticeOption
                {
                    Text = IsPinyinPractice ? i.Word : i.Pinyin,
                    IsCorrect = false
                });

            // 打乱顺序
            var mixed = distractors.Append(correct)
                                    .OrderBy(_ => _random.Next())
                                    .ToList();

            Options.Clear();
            foreach (var item in mixed)
                Options.Add(item);
        }
        private void OnOptionSelected(PracticeOption selected)
        {
            if (selected.IsCorrect)
            {
                // 可添加答对反馈逻辑
                LoadNextQuestion();
            }
            else
            {
                // 可添加错误反馈逻辑
            }
        }
    }
    public class PracticeItem
    {
        public string Pinyin { get; set; }
        public string Word { get; set; }
    }

    public class PracticeOption
    {
        public string Text { get; set; }
        public bool IsCorrect { get; set; }
    }
}
